{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常用模块\n",
    "- calendar\n",
    "- time \n",
    "- datetime\n",
    "- timeit\n",
    "- os\n",
    "- shutil\n",
    "- zip\n",
    "- math\n",
    "- string\n",
    "- 上述所有模块使用理论上都应该先导入，string是特例\n",
    "- calendar，time，datetime的区别参考中文意思"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# calendar\n",
    "- 跟日历相关的模块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import calendar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                  2018\n",
      "\n",
      "      January                   February                   March\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      " 1  2  3  4  5  6  7                1  2  3  4                1  2  3  4\n",
      " 8  9 10 11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11\n",
      "15 16 17 18 19 20 21      12 13 14 15 16 17 18      12 13 14 15 16 17 18\n",
      "22 23 24 25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25\n",
      "29 30 31                  26 27 28                  26 27 28 29 30 31\n",
      "\n",
      "       April                      May                       June\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      "                   1          1  2  3  4  5  6                   1  2  3\n",
      " 2  3  4  5  6  7  8       7  8  9 10 11 12 13       4  5  6  7  8  9 10\n",
      " 9 10 11 12 13 14 15      14 15 16 17 18 19 20      11 12 13 14 15 16 17\n",
      "16 17 18 19 20 21 22      21 22 23 24 25 26 27      18 19 20 21 22 23 24\n",
      "23 24 25 26 27 28 29      28 29 30 31               25 26 27 28 29 30\n",
      "30\n",
      "\n",
      "        July                     August                  September\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      "                   1             1  2  3  4  5                      1  2\n",
      " 2  3  4  5  6  7  8       6  7  8  9 10 11 12       3  4  5  6  7  8  9\n",
      " 9 10 11 12 13 14 15      13 14 15 16 17 18 19      10 11 12 13 14 15 16\n",
      "16 17 18 19 20 21 22      20 21 22 23 24 25 26      17 18 19 20 21 22 23\n",
      "23 24 25 26 27 28 29      27 28 29 30 31            24 25 26 27 28 29 30\n",
      "30 31\n",
      "\n",
      "      October                   November                  December\n",
      "Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su\n",
      " 1  2  3  4  5  6  7                1  2  3  4                      1  2\n",
      " 8  9 10 11 12 13 14       5  6  7  8  9 10 11       3  4  5  6  7  8  9\n",
      "15 16 17 18 19 20 21      12 13 14 15 16 17 18      10 11 12 13 14 15 16\n",
      "22 23 24 25 26 27 28      19 20 21 22 23 24 25      17 18 19 20 21 22 23\n",
      "29 30 31                  26 27 28 29 30            24 25 26 27 28 29 30\n",
      "                                                    31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# calendar :获取一年的日历字符串\n",
    "# 参数\n",
    "# w = 每个日期之间的间隔字符数\n",
    "# l = 每周所占用的行数\n",
    "# c = 每个月之间的间隔字符数\n",
    "cal = calendar.calendar(2018)\n",
    "print(cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                 2018\n",
      "\n",
      "      January                  February                  March\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      " 1  2  3  4  5  6  7               1  2  3  4               1  2  3  4\n",
      " 8  9 10 11 12 13 14      5  6  7  8  9 10 11      5  6  7  8  9 10 11\n",
      "15 16 17 18 19 20 21     12 13 14 15 16 17 18     12 13 14 15 16 17 18\n",
      "22 23 24 25 26 27 28     19 20 21 22 23 24 25     19 20 21 22 23 24 25\n",
      "29 30 31                 26 27 28                 26 27 28 29 30 31\n",
      "\n",
      "       April                     May                      June\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      "                   1         1  2  3  4  5  6                  1  2  3\n",
      " 2  3  4  5  6  7  8      7  8  9 10 11 12 13      4  5  6  7  8  9 10\n",
      " 9 10 11 12 13 14 15     14 15 16 17 18 19 20     11 12 13 14 15 16 17\n",
      "16 17 18 19 20 21 22     21 22 23 24 25 26 27     18 19 20 21 22 23 24\n",
      "23 24 25 26 27 28 29     28 29 30 31              25 26 27 28 29 30\n",
      "30\n",
      "\n",
      "        July                    August                 September\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      "                   1            1  2  3  4  5                     1  2\n",
      " 2  3  4  5  6  7  8      6  7  8  9 10 11 12      3  4  5  6  7  8  9\n",
      " 9 10 11 12 13 14 15     13 14 15 16 17 18 19     10 11 12 13 14 15 16\n",
      "16 17 18 19 20 21 22     20 21 22 23 24 25 26     17 18 19 20 21 22 23\n",
      "23 24 25 26 27 28 29     27 28 29 30 31           24 25 26 27 28 29 30\n",
      "30 31\n",
      "\n",
      "      October                  November                 December\n",
      "Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su     Mo Tu We Th Fr Sa Su\n",
      " 1  2  3  4  5  6  7               1  2  3  4                     1  2\n",
      " 8  9 10 11 12 13 14      5  6  7  8  9 10 11      3  4  5  6  7  8  9\n",
      "15 16 17 18 19 20 21     12 13 14 15 16 17 18     10 11 12 13 14 15 16\n",
      "22 23 24 25 26 27 28     19 20 21 22 23 24 25     17 18 19 20 21 22 23\n",
      "29 30 31                 26 27 28 29 30           24 25 26 27 28 29 30\n",
      "                                                  31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cal = calendar.calendar(2018, l=0, c=5)\n",
    "print(cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# isleap:判断某一年是否闰年\n",
    "calendar.isleap(2020)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# leapdays: 获取指定年份之间的闰年个数\n",
    "calendar.leapdays(1990, 2018)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function leapdays in module calendar:\n",
      "\n",
      "leapdays(y1, y2)\n",
      "    Return number of leap years in range [y1, y2).\n",
      "    Assume y1 <= y2.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(calendar.leapdays)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# datetime.datetime模块\n",
    "- 提供比较好用的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-10-15 13:14:00.489797\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime as dt\n",
    "print(dt.now())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# os-操作系统相关\n",
    "- 跟操作系统相关，主要是文件操作\n",
    "- 于系统相关的操作，主要包含在三个模块里\n",
    "    - os，操作系统目录相关\n",
    "    - os.path，系统路径相关操作\n",
    "    - shutil，高级文件操作，目录树的操作，文件复制，移动，删除\n",
    "- 路径：\n",
    "    - 绝对路径：总是从根目录上开始\n",
    "    - 相对路径：基本以当前环境为开始的一个相对的地方"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# os模块\n",
    "- getcwd（）获取当前的工作目录\n",
    "    - 格式：os.getcwd（）\n",
    "    - 返回值：当前工作目录的字符串\n",
    "    - 当前工作目录就是程序在进行文件相关操作，默认查找文件的目录\n",
    "- chdir()改变当前工作目录\n",
    "    - 格式：os.chdir（路径）\n",
    "    - 返回值：无\n",
    "- listdir()获取一个目录中所有子目录和文件的名称列表\n",
    "    - 格式os.listdir（路径）\n",
    "    - 返回值：所有子目录和文件名称的列表\n",
    "- makedirs()递归创建文件夹\n",
    "    - 格式：od.makedirs（递归路径）\n",
    "    - 返回值：无\n",
    "    - 递归路径：多个文件夹层层包含的路径就是递归路径，如a/b/c。。。。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['.eclipse', 'Music', '.docker', '.zcompdump-Cheng的MacBook Pro-5.3', 'tutorial', '.vim', '.DS_Store', '面向对象-2.ipynb', '.CFUserTextEncoding', '.tooling', '数据结构.ipynb', '.zshrc', '.swp', '包.ipynb', 'Pictures', '.bundle', '.zsh_history', '.p2', '.ipython', 'Desktop', 'Library', '.bash-git-prompt', 'eclipse-workspace', '.oracle_jre_usage', '.cups', '.bash_sessions', 'PycharmProjects', 'Public', '常用模块.ipynb', '面向对象.ipynb', '.python_history-01188.tmp', '.ssh', 'Movies', '.vimrc', '.dbshell', '类-成员描述符.ipynb', '.Trash', '.ipynb_checkpoints', '.jupyter', 'ghostdriver.log', 'Documents', '异常处理.ipynb', '.mysql_history', '.vscode', '正则.ipynb', '.bash_profile', '.gem', '.oh-my-zsh', 'data', '.rediscli_history', 'Downloads', '.python_history', '.gitconfig', '.mongorc.js', '.bash_history', '.viminfo', '.3T']\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.getcwd()\n",
    "ld = os.listdir()\n",
    "print(ld)\n",
    "rst = os.makedirs('cheng')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "# system()运行系统shell命令\n",
    "# 格式：os.system（系统命令）\n",
    "# 返回值：打开一个shell或者终端界面\n",
    "rst = os.system('ls')\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 值部分\n",
    "- os.curdir: current dir当前目录\n",
    "- os.pardir：parent dir，父亲目录\n",
    "- os.sep：当前系统的路径分隔符\n",
    "    - windows:\"\\\\\"\n",
    "    - linux:'/'\n",
    "- os.linesep：当前系统的换行符号\n",
    "    - windows:'\\r\\n'\n",
    "    - unix,linux,macos:'\\n'\n",
    "- os.name：当前系统名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\n",
      ".\n",
      "/\n",
      "\n",
      "\n",
      "posix\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "print(os.pardir)\n",
    "print(os.curdir)\n",
    "print(os.sep)\n",
    "print(os.linesep)\n",
    "print(os.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# shutil模块\n",
    "- copy（）复制文件\n",
    "- 格式：shutil.copy（来源路径，目标路径）\n",
    "- 返回值：返回目标路径\n",
    "- 拷贝的同时，可以给文件重命名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- copyfile()将一个文件中的内容拷贝到另一个文件中去\n",
    "    - 格式：shutil.copyfile（‘原路径’，‘目标路径’）\n",
    "    - 返回值：无\n",
    "- move()移动文件/文件夹\n",
    "    - 格式shutil.move（‘源文件’，‘目标路径’\n",
    "    - 返回值：目标路径"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 归档和压缩\n",
    "- 归档：把多个文件或者文件夹合并到一个文件当中\n",
    "- 压缩：用算法把多个文件或者文件夹无损或者有损合并到一个文件当中\n",
    "- 返回值：归档之后的地址\n",
    "    - make_archive（）归档操作\n",
    "    - 格式：shutil.make_archive('归档之后的目录和文件名','后缀','需要归档的文件夹')\n",
    "    - 返回值：归档之后的地址\n",
    "    - 后缀只能是;zip,tar,gztar\n",
    "- unpack_archive()解包操作\n",
    "    - 格式：shutil.unpack_archive（‘归档文件地址’，‘解包之后的地址’）\n",
    "    - 返回值：解包之后的地址"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# zip 压缩包\n",
    "- 模块名称叫zipfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
